﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel;
using Model;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 系统仓储接口实现
    /// </summary>
    public class SysSystemRepository : Repository<SysSystem>, ISysSystemRepository
    {
        /// <summary>
        /// 获取项目系统
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="authWhere">权限条件</param>
        /// <returns></returns>
        public List<SysSystemOutPut> GetSystemList(string? corporationKey, string? authWhere)
        {
            StringBuilder sql = new(@" select ss.*,su.UserName,sc.CorporationName,sc.CorporationCode from Sys_System as  ss
                            inner join Sys_User as su  on ss.CreateUser = su.UserId
                            inner join  Sys_Corporation as sc  on  sc.CorporationKey = ss.CorporationKey  ");
            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey) && Const.XC_CorporationKey != corporationKey)
            {
                qp.listWhere.Append(" and sc.CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrWhiteSpace(authWhere))
            {
                qp.listWhere.Append(authWhere);
            }
            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            sql.Append("  order by ss.CreateTime desc");
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysSystemOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 根据公司名称和编码，获取公司数据
        /// </summary>
        /// <param name="name">公司名称</param>
        /// <param name="code">公司编码</param>
        /// <returns>返回指定数据</returns>
        public List<SysSystem> GetSystemMsg(string? name, string? code)
        {
            string sql = @" select * from Sys_System where SystemName = @SystemName OR SystemCode=@SystemCode ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysSystem>(sql, new { SystemName = name, SystemCode = code }).ToList();
        }

        /// <summary>
        /// 设置系统是否启用
        /// </summary>
        /// <param name="keys">key集合</param>
        /// <param name="isOpen">是否启用</param>
        public void SetSystemIsOpen(List<string> keys, bool isOpen)
        {
            string sql = "   update Sys_System set IsOpen=@IsOpen where SystemKey in @SystemKey  ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Query(sql, new { SystemKey = keys, IsOpen = isOpen });
        }
    }
}
